package com.urea.web.controller.urea.api;

import com.alibaba.fastjson2.JSONObject;
import com.urea.common.annotation.Anonymous;
import com.urea.common.core.domain.AjaxResult;
import com.urea.common.core.domain.entity.SysUser;
import com.urea.common.exception.ServiceException;
import com.urea.system.service.IFleetUserService;
import com.urea.system.service.ISysUserService;
import com.urea.system.service.IWxMpService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.BaseWxMpServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 类描述:
 *
 * @author fangXu
 * @version 1.0
 * @date 2023/8/31 10:28
 */
@RestController
@RequestMapping("/mp")
@Slf4j
@Api(tags = "公众号")
public class WxMpController {

    @Autowired
    private WxMpService mpService;
    @Autowired
    private IWxMpService wxMpService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private IFleetUserService fleetUserService;


    /**
     * 微信公众号消息验证
     * @param signature 签名
     * @param timestamp 时间错
     * @param nonce nonce
     * @param echostr 随机串
     * @return 合法返回随机串
     */
    @GetMapping("/receiveMsg")
    public String msgController(@RequestParam("signature")String signature, @RequestParam("timestamp") String timestamp,
                              @RequestParam("nonce")String nonce, @RequestParam("echostr") String echostr){
        log.info("############接受到数据:{}##############",signature);
        boolean b = mpService.checkSignature(timestamp, nonce, signature);
        if (!b){
            throw new ServiceException("消息校验异常!");
        }else{
            log.info("##################消息合法###############");
        }
        return echostr;
    }


    /**
     * 公众号数据统计获取订单数据 终端数量
     * @return 结果
     */
    @GetMapping("/statistics")
    @ApiOperation("数据统计-终端数据及订单数量")
    public AjaxResult getStatistics(){
        return AjaxResult.success(wxMpService.getStatistics());
    }

    /**
     * 获取用户的openId
     * @param code code
     * @return 结果
     */
    @GetMapping("/getOpenId")
    @ApiOperation("根据code获取用户openId")
    public AjaxResult getOpenId(String code){
        wxMpService.getOpenId(code);
        return AjaxResult.success();
    }
    @GetMapping("/wx/bind")
    @ApiOperation("绑定微信")
    public AjaxResult wxBind(String code,String state){
        //state就是用户id
        userService.updateOpenId(code,state);
        return AjaxResult.success();
    }

    /**
     * 根据手机号获取详细信息
     */
    @GetMapping(value = {"/getUserByPhone" })
    @ApiOperation("个人中心")
    public AjaxResult getInfo(@RequestParam("phone") String phone)
    {
        AjaxResult ajax = AjaxResult.success();
        SysUser sysUser = userService.selectUserByUserName(phone);
        ajax.put(AjaxResult.DATA_TAG, sysUser);
        return ajax;
    }

    /**
     * 获取登录用户角色及所在车队id
     * @return 信息
     */
    @GetMapping("/queryRoleAndFleetId")
    @ApiOperation("获取登录用户角色及所在车队id")
    public AjaxResult queryRoleAndFleetId(){
        return AjaxResult.success(fleetUserService.queryRoleAndFleetId());
    }



}
